﻿using BCData.Common.UploadFileRecord;
using BCDto.Common.UploadFileRecord;
using BCEntity.Common.UploadFileRecord;
using BCService.Utils;
using BCService.WeChatComponent.WeChatMaterialService.WeChatNewsMaterialService;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.Common.UploadFileRecord
{
    /// <summary>
    /// 上传文件记录服务
    /// </summary>
    public class UploadFileRecordService : IUploadFileRecordService
    {
        private readonly IUploadFileRecordData uploadFileRecordData;
        private readonly IWeChatNewsMaterialService weChatNewsMaterialService;

        public UploadFileRecordService(IUploadFileRecordData uploadFileRecordData,
            IWeChatNewsMaterialService weChatNewsMaterialService)
        {
            this.uploadFileRecordData = uploadFileRecordData;
            this.weChatNewsMaterialService = weChatNewsMaterialService;
        }

        public UploadFileRecordDto Add(UploadFileRecordDto uploadFileRecordDto)
        {
            UploadFileRecordEntity uploadFileRecord = this.uploadFileRecordData.GetEntity(uploadFileRecordDto.UploadFileRecordNo).GetAwaiter().GetResult();
            if (uploadFileRecord != null)
            {
                return uploadFileRecord.As<UploadFileRecordDto>();
            }
            //允许上传的类型
            Dictionary<string, string> allowType = new Dictionary<string, string>();
            allowType.Add(".jpg", "image");
            allowType.Add(".jpeg", "image");
            allowType.Add(".gif", "image");
            allowType.Add(".bmp", "image");
            allowType.Add(".png", "image");

            allowType.Add(".doc", "office");
            allowType.Add(".docx", "office");
            allowType.Add(".xls", "office");
            allowType.Add(".xlsx", "office");
            allowType.Add(".csv", "office");
            allowType.Add(".ppt", "office");
            allowType.Add(".pptx", "office");
            allowType.Add(".pub", "office");
            allowType.Add(".txt", "office");
            allowType.Add(".md", "office");
            allowType.Add(".xml", "office");

            allowType.Add(".rar", "office");
            allowType.Add(".zip", "office");
            allowType.Add(".tar", "office");
            allowType.Add(".gz", "office");
            allowType.Add(".7z", "office");
            allowType.Add(".bz2", "office");
            allowType.Add(".cab", "office");
            allowType.Add(".iso", "office");

            allowType.Add(".flv", "video");
            allowType.Add(".swf", "video");
            allowType.Add(".mkv", "video");
            allowType.Add(".avi", "video");
            allowType.Add(".rm", "video");
            allowType.Add(".rmvb", "video");
            allowType.Add(".mpeg", "video");
            allowType.Add(".mpg", "video");
            allowType.Add(".ogg", "video");
            allowType.Add(".ogv", "video");
            allowType.Add(".mov", "video");
            allowType.Add(".wmv", "video");
            allowType.Add(".webm", "video");
            allowType.Add(".mp4", "video");
            allowType.Add(".mp3", "voice");
            allowType.Add(".amr", "voice");
            allowType.Add(".wav", "voice");
            allowType.Add(".wma", "voice");
            allowType.Add(".mid", "voice");

            allowType.Add(".ttf", "font");
            allowType.Add(".eot", "font");
            allowType.Add(".svg", "font");
            allowType.Add(".woff", "font");

            allowType.Add(".css", "css");
            allowType.Add(".js", "js");
            UploadFileRecordEntity uploadFileRecordEntity = uploadFileRecordDto.As<UploadFileRecordEntity>();
            uploadFileRecordEntity.FileType = allowType[uploadFileRecordEntity.FileSuffix];
            return this.uploadFileRecordData.Add(uploadFileRecordEntity).GetAwaiter().GetResult().As<UploadFileRecordDto>();
        }

        public bool Delete(string uploadFileRecordNo)
        {
            return this.uploadFileRecordData.Delete(uploadFileRecordNo).GetAwaiter().GetResult();
        }

        public IEnumerable<UploadFileRecordDto> GetNoneTransportList()
        {
            return this.uploadFileRecordData.GetNoneTransportListAsync().GetAwaiter().GetResult().As<IEnumerable<UploadFileRecordDto>>();
        }

        public IEnumerable<UploadFileRecordDto> GetUploadFileRecords(string fileType, int index, int size, out int total)
        {
            var result = this.uploadFileRecordData.GetUploadFileRecords(fileType, index, size).GetAwaiter().GetResult();
            total = result.Item2;
            return result.Item1.As<IEnumerable<UploadFileRecordDto>>();
        }

        public bool UpdateTransported(string uploadFileRecordNo)
        {
            return this.uploadFileRecordData.UpdateTransportedAsync(uploadFileRecordNo).GetAwaiter().GetResult();
        }
    }
}
